﻿using System;
using System.Drawing;
using System.Web.Mvc;

namespace OneWorks.Web.Common
{
    public class VerificationCodeParameters
    {
        public string Code { get; set; }

        public VerificationCodeType Type { get; set; }
    }

    public enum VerificationCodeType
    {
        Letters,
        Numbers,
        Chinese,
        Confusion
    }

    public class VerificationCodeHandler
    {
        private readonly VerificationCodeParameters _parameters;

        public string Code
        {
            get
            {
                return this._parameters.Code;
            }
        }

        public VerificationCodeHandler(VerificationCodeParameters parameters)
        {
            this._parameters = parameters;
        }
        /// <summary>
        /// 画图片的背景图，干扰
        /// </summary>
        /// <returns></returns>
        private Bitmap CreateImages()
        {
            int step = 0;
            if (this._parameters.Type == VerificationCodeType.Chinese)
            {
                step = 5;//中文字符比较大，所以字距要比较大
            }
            int iwidth = this._parameters.Code.Length * (13 + step);
            Bitmap image = new Bitmap(iwidth, 22);
            Graphics g = Graphics.FromImage(image);
            g.Clear(Color.White);//清除背景色
            Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };//定义随机颜色
            string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
            Random rand = new Random();
            for (int i = 0; i < 50; i++)
            {
                int x1 = rand.Next(image.Width);
                int x2 = rand.Next(image.Width);
                int y1 = rand.Next(image.Height);
                int y2 = rand.Next(image.Height);
                g.DrawLine(new Pen(Color.LightGray, 1), x1, y1, x2, y2);//根据坐标画线
            }

            for (int i = 0; i < this._parameters.Code.Length; i++)
            {
                int cindex = rand.Next(7);
                int findex = rand.Next(5);

                Font f = new Font(font[findex], 10, FontStyle.Bold);
                Brush b = new SolidBrush(c[cindex]);
                int ii = 4;
                if ((i + 1) % 2 == 0)
                {
                    ii = 2;
                }
                g.DrawString(this._parameters.Code.Substring(i, 1), f, b, 3 + (i * (12)), ii);
            }

            g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1);
            g.Dispose();
            return image;
        }

        public FileContentResult GetFileContentResult()
        {
            Bitmap bitmap = this.CreateImages();
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Gif);
            bitmap.Dispose();
            return new FileContentResult(memoryStream.ToArray(), "image/gif");
        }
    }
}
